package com.niiwoo.civet.trade.listener;

import com.alibaba.fastjson.JSON;
import com.niiwoo.civet.trade.constant.MqConstant;
import com.niiwoo.civet.trade.dao.entity.ProjectPackage;
import com.niiwoo.civet.trade.service.local.intelligenceInvest.ProjectPackageLocalService;
import com.niiwoo.tripod.provider.exception.BizException;
import com.rabbitmq.client.Channel;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;

@Component
public class ProjectPackageListener {

	private static final Logger logger = LoggerFactory.getLogger(ProjectPackageListener.class);
	
    @Autowired
    private ProjectPackageLocalService projectPackageLocalService;
    
    @RabbitListener(queues = {MqConstant.PROJECT_PACKAGE_PACKAGING_QUEUE})
    public void packagingHandler(Long packageConfigId, Message message, Channel channel) throws IOException {
        //应答标记
        Boolean isAck = true;
        try {
        	logger.info("packagingHandler packageConfigId:{}",packageConfigId);
            projectPackageLocalService.doPackageForConfig(packageConfigId);
        } catch (BizException e) {
        	logger.error("packagingHandlerBizException", e);
        } catch (Exception e) {
        	logger.error("packagingHandlerException", e);
            isAck = false;
	        try {
		        Thread.sleep(5 * 1000);
	        } catch (Exception expect) {
	        }
        } finally {
            if (isAck) {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        }
    }

    @RabbitListener(queues = {MqConstant.PROJECT_PACKAGE_PUBLISHING_QUEUE})
    public void publishingHandler(Long packageId, Message message, Channel channel) throws IOException {
        //应答标记
        Boolean isAck = true;
        try {
            logger.info("publishingHandler packageId:{}",packageId);
            projectPackageLocalService.doPublish(packageId);
        } catch (BizException e) {
            logger.error("publishingHandlerBizException", e);
        } catch (Exception e) {
            logger.error("publishingHandlerException", e);
            isAck = false;
            try {
                Thread.sleep(5 * 1000);
            } catch (Exception expect) {
            }
        } finally {
            if (isAck) {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        }
    }

    @RabbitListener(queues = {MqConstant.PROJECT_PACKAGE_PUBLISHING_FOR_FULL_QUEUE})
    public void publishingForFullHandler(ProjectPackage projectPackage, Message message, Channel channel) throws IOException {
        //应答标记
        Boolean isAck = true;
        try {
            logger.info("publishingForFullHandler projectPackage:{}", JSON.toJSON(projectPackage));
            projectPackageLocalService.doPublishForFull(projectPackage);
        } catch (BizException e) {
            logger.error("publishingForFullHandler", e);
        } catch (Exception e) {
            logger.error("publishingForFullHandler", e);
            isAck = false;
            try {
                Thread.sleep(5 * 1000);
            } catch (Exception expect) {
            }
        } finally {
            if (isAck) {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        }
    }

    @RabbitListener(queues = {MqConstant.PROJECT_PACKAGE_ADD_PROJECT_QUEUE})
    public void addProjectHandler(Long projectId, Message message, Channel channel) throws IOException {
        //应答标记
        Boolean isAck = true;
        try {
            logger.info("addProjectHandler packageId:{}",projectId);
            projectPackageLocalService.addProject(projectId);
        } catch (BizException e) {
            logger.error("addProjectHandlerBizException", e);
        } catch (Exception e) {
            logger.error("addProjectHandlerException", e);
            isAck = false;
            try {
                Thread.sleep(5 * 1000);
            } catch (Exception expect) {
            }
        } finally {
            if (isAck) {
                channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
            } else {
                channel.basicNack(message.getMessageProperties().getDeliveryTag(), false, true);
            }
        }
    }

}
